# function to generate initial values for MCMC estimation of the dynamic linear model
inits.fun <- function(seed1, seed2, i) {
  set.seed(seed1)
  list(beta = matrix(rnorm(m - 1, 0, 0.1), m - 1, l), 
       Omega.star = runif(m - 1, 0.5, 1.5), 
       theta.star = matrix(rnorm(n * (m - 1)), n, m - 1), 
       .RNG.name = ifelse(i == 1, "base::Wichmann-Hill", 
                          ifelse(i == 2, "base::Marsaglia-Multicarry", 
                                 "base::Super-Duper")), 
       .RNG.seed = seed2)
}

# function to summarize posterior draws
posterior.summary <- function(x, var.names = NULL, prob = 0.95) {
  if (is.matrix(x)) {
    posterior.mean <- colMeans(x)
    HPD.interval <- HPDinterval(mcmc(x), prob = prob)
    result <- cbind(posterior.mean, HPD.interval)
    rownames(result) <- var.names
  } else {
    posterior.mean <- mean(x)
    HPD.interval <- HPDinterval(mcmc(x), prob = prob)
    result <- c(posterior.mean, HPD.interval)
  }
  result
}

# function to draw lines for point estimates and credible intervals
line.fun <- function(x, y) {
  lines(x, y[, 2], lty = 3)
  lines(x, y[, 3], lty = 3)
  lines(x, y[, 1])
}
line.fun.2 <- function(x, y) {
  HPD <- HPDinterval(mcmc(y))
  polygon(c(x, rev(x)), c(y[, 2], rev(y[, 3])), 
          border = NA, col = "gray80")
  lines(x, y[, 1], lty = 2)
}